<template>
  <div>
    <el-form :model="searchForm" inline>
      <el-form-item label="分类名称">
        <el-input placeholder="分类名称" clearable v-model="searchForm.categoryName"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select placeholder="选择状态" clearable v-model="searchForm.status">
          <el-option label="可用" :value="1"/>
          <el-option label="禁用" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button @click="openDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="categoryId" label="编号" align="center"/>
      <el-table-column prop="categoryName" label="分类名称" align="center"/>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">可用</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row.categoryId)">修改</el-button>
          <el-button size="small" type="warning" @click="deleteInfo(scope.row.categoryId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :total="pageInfo.total" :page-sizes="[5,10,20,50,100]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"/>
    <CategoryUpdate @onCloseDialog="getDataList" ref="categoryUpdate"/>
  </div>
</template>

<script>
import CategoryUpdate from '@/views/admin/category/CategoryUpdate.vue'
export default {
  name: 'CategoryList',
  components: { CategoryUpdate },
  data () {
    return {
      searchForm: {
        status: '',
        categoryName: ''
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: []
    }
  },
  methods: {
    openDialog (categoryId) {
      this.$refs.categoryUpdate.init(categoryId)
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号为[${id}]的数据吗？`).then(() => {
        this.$http.delete(`category/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        })
      }).catch(() => {})
    },
    currentChange (val) {
      this.pageInfo.page = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageInfo.limit = val
      this.getDataList()
    },
    getDataList () {
      const params = {
        ...this.pageInfo,
        ...this.searchForm
      }
      this.$http.get('category/page', params).then(({ data }) => {
        this.dataList = data.records
        this.pageInfo.page = data.current
        this.pageInfo.total = data.total
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
    })
  }
}
</script>

<style scoped>

</style>
